/*
 * @Description: 
 * @Author: BaldHead
 * @Date: 2022-03-26 20:22:22
 * @LastEditTime: 2022-03-29 13:48:52
 * @LastEditors: Please set LastEditors
 */
//数组去重
let ary = [12, 23, 12, 15, 25, 23, 25, 14, 16];

// set - es6 set没有重复元素
let arr1 = [...new Set(ary)];
let arr2 = Array.from(new Set(ary));
console.log(arr1, arr2);
//那当前项和后面的内容进行比较
for (let i = 0; i < ary.length - 1; i++) {
    let item = ary[i];
    args = ary.slice(i + 1);
    if (args.indexOf(item) > -1) { //includes
        //包含：1.当前项删去/赋null值  2.新数组
        //splice 删除 
        //1.原数组改变，这样如果I继续++，会导致数组塌陷
        //2.性能不好，当前项一旦删除，后面项索引都要变
        ary.splice(i, 1); //数组塌陷,splice会对原来数组改变   吧   
        i--; //splice解决数组塌陷
    }

}
//创建新数组
ary = [12, 23, 12, 15, 25, 23, 25, 14, 16];
let arr3 = [...ary];
let arr4 = []; //开了个新内存
for (let i = 0; i < ary.length - 1; i++) {
    let item = ary[i];
    args = ary.slice(i + 1);
    if (args.indexOf(item) > -1) { //includes
        //赋值为Null
        arr3[i] = null;
    } else {
        arr4.push(item)
    }
}
arr3 = arr3.filter(item => {
    return item
})
console.log(arr3, ary);
arr4.push(ary[ary.length - 1])
console.log(arr4, ary);

// for (let i = 0; i < ary.length; i++) {
//     let item = ary[i];
//     args = ary.slice(i + 1);
//     if (args.indexOf(item) > -1) { //includes

//     } else {
// arr4.push(item)
//     }
// }
let ary1 = [12, 23, 12, 15, 25, 23, 25, 14, 16];
for (let i = 0; i < ary1.length - 1; i++) {
    let item = ary1[i];
    args = ary1.slice(i + 1);
    if (args.includes(item)) {
        ary1[i] = ary1[ary1.length - 1];
        ary1.length--;
        i--;
    }

}
console.log(ary1);
let obj = {};
let ary2 = [12, 23, 12, 15, 25, 23, 25, 14, 16];
for (let i = 0; i < ary2.length; i++) {
    let item = ary2[i];
    if (typeof obj[item] !== "undefined") {
        ary2[i] = ary2[ary2.length - 1];
        ary2.length--;
        i--;
        continue;
    }
    obj[item] = item;
}
console.log("键值对", ary2);
//排序  
//排序 +前一项和后一项比
//排序加正则
ary = [12, 23, 12, 15, 25, 23, 25, 14, 16];
ary.sort((a, b) => a - b); //排序

let str = ary.join('@') + '@';
let reg = /(\d+@)\1*/g //\1分组引用 *0~多次
let arr = [];
str.replace(reg, (val, group1) => {
    arr.push(parseFloat(group1.slice(0, group1.length - 1)))
})
console.log(arr);

// 数组去重
/*
 *@Description: set数组去重
 *@FuncAuthor: BaldHead
 *@CreateDate: 2022-03-29 12:45:32
 *@LastEditTime: 
 *@LastEditors: 
 */
// let ary = [12, 23, 12, 15, 25, 23, 25, 14, 16];
// console.log([...new Set(ary)]);
// console.log(Array.from(new Set(ary)));

/*
 *@Description: 双for includes indexOf
 *@FuncAuthor: BaldHead
 *@CreateDate: 2022-03-29 12:48:16
 *@LastEditTime: 
 *@LastEditors: 
 */

function dedup1(array) {
    for (let index = 0; index < array.length; index++) {
        const element = array[index];
        args = array.slice(index + 1);
        if (args.includes(element)) {
            array.splice(index, 1);
            index--;
        }
    }
    return array;
}
console.log("dedup1", dedup1([12, 23, 12, 15, 25, 23, 14, 16]));


function dedup2(array) {
    // let arrar = [];
    for (let index = 0; index < array.length; index++) {
        const element = array[index];
        args = array.slice(index + 1);
        if (args.includes(element)) {
            array[index] = null;
        } else {
            // arrar.push(element)
        }
    }
    return array.filter(item => {
        return item;
    });
}
console.log("dedup2", dedup2([12, 23, 12, 15, 25, 23, 14, 16]));



function dedup3(array) {
    // let arrar = [];
    for (let index = 0; index < array.length; index++) {
        const element = array[index];
        args = array.slice(index + 1);
        if (args.includes(element)) {
            array[index] = array[array.length - 1];
            array.length--;
            index--;
        }
    }
    return array.filter(item => {
        return item;
    });
}
console.log("dedup3", dedup3([12, 23, 12, 15, 25, 23, 14, 16]));

/*
 *@Description: 键值对
 *@FuncAuthor: BaldHead
 *@CreateDate: 2022-03-29 13:02:17
 *@LastEditTime: 
 *@LastEditors: 
 */


function dedup4(array) {
    // let arrar = [];
    let obj = {};
    for (let index = 0; index < array.length; index++) {
        const element = array[index];
        if (typeof obj[element] !== "undefined") {
            array[index] = array[array.length - 1];
            array.length--;
            index--;
        } else {
            obj[element] = element;
        }
    }
    return array;
}
console.log("dedup4", dedup4([12, 23, 12, 15, 25, 23, 14, 16]));

/*
 *@Description: 排序 前后比较/正则
 *@FuncAuthor: BaldHead
 *@CreateDate: 2022-03-29 13:09:57
 *@LastEditTime: 
 *@LastEditors: 
 */

function dedup5(array) {
    array.sort((a, b) => a - b)
    let str = array.join('@') + '@';
    // let reg = /(\d+@)\1*/g
    let pattern = /(\d+@)\1*/g;
    let arr = [];
    str.replace(pattern, (val, group1) => {
        return arr.push(parseFloat(group1.substr(0, group1.length)));
    })
    return arr;
}
console.log("dedup5", dedup5([12, 23, 12, 15, 25, 23, 14, 16]));